/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// gui_controls2.cpp - 

#include "ui_local.h"

#if 0

/*-----------------------------------------------------------------------------
	Abstract Control :
-----------------------------------------------------------------------------*/

EGUIControl::EGUIControl(EGUIManager *fm, const char *name, float x, float y, float w, float h)
 : EGUIFrame(fm, name, x, y, w, h)
{
	highlight	=	0;
	var			=	NULL;
	shell->AddCommand(ObjName("setText"), (EShellCmd_f)SetText_f,	this);
	shell->AddCommand(ObjName("setVar"),  (EShellCmd_f)SetVar_f,		this);
}


EGUIControl::~EGUIControl(void)
{
	shell->RemoveCommand(ObjName("setText"));
	shell->RemoveCommand(ObjName("setVar"));
}


void EGUIControl::SetText_f(EGUIControl *self, int argc, char **argv)
{
	IConsole	*con = self->con;
	if (argc!=2) {	
		LOGF("usage : setText <var name>\r\n");
		return;
	}
	
	self->text = argv[1];
}


void EGUIControl::SetVar_f(EGUIControl *self, int argc, char **argv)
{
	IConsole	*con = self->con;
	if (argc!=2) {	
		LOGF("usage : setVar <var name>\r\n");
		return;
	}
	
	self->var = self->shell->RegisterVar(argv[1], "", 0);
}


void EGUIControl::FMCtrlReadConfig(void)
{
	if (var) {
		value = var->String();
	} else {
		value = "";
	}
}


void EGUIControl::FMCtrlWriteConfig(void)
{
	if (var) {
		var->SetValue(value.c_str());
	}
}

uiRect_s EGUIControl::GetTextRect(void)
{
	float spacing = decor->GetTextCtrlSpacing();
	uiRect_s	r = GetFrameRect();
	
	r.w =  r.w / 2.0f;
	r.w -= (2*spacing);
	r.h -= (2*spacing);
	r.x += spacing;
	r.y += spacing;
	return r;
}

uiRect_s EGUIControl::GetCtrlRect(void)
{
	float spacing = decor->GetTextCtrlSpacing();
	uiRect_s	r = GetFrameRect();

	r.w =  r.w / 2.0f;
	r.x += r.w;
	r.w -= (2*spacing);
	r.h -= (2*spacing);
	r.x += spacing;
	r.y += spacing;
	return r;
}


void EGUIControl::FMProcess(float delta, float time)
{
	highlight -= delta / decor->GetFadeOutTime() ;
	highlight = Clamp<float>(highlight, 0, 1);
	
	flicker_time = time;
	
	if (fm->GetFrameUnderCursor()==this) {
		highlight	=	1;
	}
}


void EGUIControl::FMPaint(void)
{
	float	spacing		=	decor->GetTextCtrlSpacing();
	EVec4	color_bg	=	decor->GetControlColorBackground();
	EVec4	color_fg	=	decor->GetControlColorForeground();
	
	const char *font_name =	decor->GetSmallFont();

	color_fg	=	Vec4Lerp(color_fg, decor->GetControlColorHighlight(), highlight);
		
	if (fm->GetActiveFrame()==this) {
		color_fg = decor->GetControlColorActivated();
	}
	
	uiRect_s r  = GetFrameRect();
	decor->DrawBorderRect(r.x, r.y, r.w, r.h, BORDER_SOLID,		color_bg);
	decor->DrawBorderRect(r.x, r.y, r.w, r.h, BORDER_OUTLINE,	color_fg);
	
	uiRect_s rt = GetTextRect();
	
	EFont *font		 = ui->RegisterFont(font_name);
	decor->DrawTextInRect(rt.x, rt.y, rt.w, rt.h, font_name, text.c_str(), EVec4(1,1,1,1), GUI_ALIGN_RIGHT);
	
	EGUIFrame::FMPaint();
}


/*-----------------------------------------------------------------------------
	EDIT BOX :
-----------------------------------------------------------------------------*/

EGUIEditBox::EGUIEditBox(EGUIManager *fm, const char *name, float x, float y, float w, float h)
 : EGUIControl(fm, name, x,y,w,h)
{
}


EGUIEditBox::~EGUIEditBox(void)
{
}


void EGUIEditBox::FMKeyDown(uint code)
{
	if (code==KEY_BACKSPACE) {
		if (value.length()==0) {
			return;
		}
		value.resize(value.length()-1);
	}
	if (code==KEY_ENTER) {
		fm->SetActiveFrame(GetParent());
	}
}


void EGUIEditBox::FMKeyPress(uint code)
{
	if (isprint(code)) {
		value.push_back(code);
	}
}


void EGUIEditBox::FMPaint(void)
{
	EGUIControl::FMPaint();

	float spacing = decor->GetTextCtrlSpacing();
	const char *font_name =	decor->GetSmallFont();

	uiRect_s r  = GetFrameRect();
	
	uiRect_s tr = GetCtrlRect();

	const char *text = value.c_str();
	EFont *font		 = ui->RegisterFont(font_name);
	float text_len	 = ui->GetStringLengthAdv(font, text);
	float lh = font->GetLineHeight();
	
	if (fm->GetActiveFrame()==this) {
		//	find first character :
		while (text_len + font->GetXAdvance(' ') > tr.w) {
			text++;
			text_len = ui->GetStringLengthAdv(font, text);
			if (text_len==0) {
				break;
			}
		}
		
		decor->DrawTextInRect(tr.x, tr.y, tr.w, tr.h, font_name, text, EVec4(1,1,1,1), GUI_ALIGN_CENTER);

		//	draw cursor :
		//float cur_pos_x = tr.x + text_len;				// Left align
		float cur_pos_x = tr.x + 0.5*(tr.w + text_len);		// Center align
		float cur_pos_y = floor(r.y + (r.h + lh) / 2.0);
		float alpha		= 0.5f + 0.5f * sin(10*flicker_time);
		
		quad_s q;
		q.color = EVec4(1,1,1, alpha);
		q.uv0 = q.uv1 = EVec2(0,0);
		q.p0  = EVec2(cur_pos_x, tr.y);
		q.p1  = EVec2(cur_pos_x + font->GetXAdvance(' '), tr.y + tr.h);
		rs->DrawQuadList(1, &q, "*white");
		
	} else {
		decor->DrawTextInRect(tr.x, tr.y, tr.w, tr.h, font_name, text, EVec4(1,1,1,1), GUI_ALIGN_CENTER);
	}
}


/*-----------------------------------------------------------------------------
	CHECK BOX :
-----------------------------------------------------------------------------*/

EGUICheckBox::EGUICheckBox(EGUIManager *fm, const char *name, float x, float y, float w, float h)
 : EGUIControl(fm, name, x,y,w,h)
{
}


EGUICheckBox::~EGUICheckBox(void)
{
}


void EGUICheckBox::FMMouseClick(uint button, float x, float y)
{
	EGUIFrame::FMMouseClick(button, x, y);
	uint  int_val = atoi(value.c_str());
	if (int_val) {
		value = "0";
	} else {
		value = "1";
	}
}


void EGUICheckBox::FMPaint(void)
{
	EGUIControl::FMPaint();

	float spacing = decor->GetTextCtrlSpacing();
	const char *font_name =	decor->GetSmallFont();

	uiRect_s r  = GetFrameRect();
	
	uiRect_s tr = GetCtrlRect();

	uint  int_val = atoi(value.c_str());
	
	if (int_val) {	
		decor->DrawTextInRect(tr.x, tr.y, tr.w, tr.h, font_name, "Yes", EVec4(1,1,1,1), GUI_ALIGN_CENTER);
	} else {
		decor->DrawTextInRect(tr.x, tr.y, tr.w, tr.h, font_name, "No", EVec4(1,1,1,1), GUI_ALIGN_CENTER);
	}
}


/*-----------------------------------------------------------------------------
	SLIDER :
-----------------------------------------------------------------------------*/

#define SLIDER_SCALE	"ui/slider_scale.tga"
#define SLIDER_GLOW		"ui/slider_glow.tga"
#define SLIDER_BULLET	"ui/slider_bullet.tga"

EGUISlider::EGUISlider(EGUIManager *fm, const char *name, float x, float y, float w, float h)
 : EGUIControl(fm, name, x,y,w,h)
{
	min_val = 0;
	max_val = 1;
	prev_val = 0;
	shell->AddCommand(ObjName("setMinMax"), (EShellCmd_f)SetMinMax_f, this);
}


EGUISlider::~EGUISlider(void)
{
	shell->RemoveCommand(ObjName("setMinMax"));
}


void EGUISlider::SetMinMax_f(EGUISlider *self, int argc, char **argv)
{
	IConsole	*con = self->con;
	if (argc!=3) {	
		LOGF("usage : setVar <min> <max>\r\n");
		return;
	}
	self->min_val	=	(float)atof(argv[1]);
	self->max_val	=	(float)atof(argv[2]);
}


void EGUISlider::FMProcess(float delta, float time)
{				
	float cur_val = (float)atof(value.c_str());
	prev_val = prev_val + (cur_val - prev_val) * (1-exp(-delta/0.05));
}


void EGUISlider::FMKeyDown(uint code)
{
	
}


void EGUISlider::FMMouseClick(uint button, float x, float y)
{
	EGUIFrame::FMMouseClick(button, x, y);

	uiRect_s tr = GetCtrlRect();
	uiRect_s r  = GetFrameRect();
	
	float rx = x + r.x;
	float ry = y + r.y;

	IImage	*slider_scale	=	rs->RegisterImage(SLIDER_SCALE);
	IImage	*slider_bullet	=	rs->RegisterImage(SLIDER_BULLET);
	float scale_w = slider_scale->GetWidth();
	float scale_h = slider_scale->GetHeight();
	
	float start_pos  = ceil(tr.x + (tr.w-scale_w)/2.0f);
	float end_pos    = start_pos + scale_w;
	float percentage = 0;
	
	if (start_pos <= rx && rx <= end_pos)	 {
		percentage = (rx-start_pos) / (end_pos-start_pos);
	}
	if (rx<start_pos) {
		percentage = 0;
	}
	if (rx>end_pos) {
		percentage = 1;
	}
	value	=	va("%f", min_val + percentage * (max_val-min_val));
}


void EGUISlider::FMPaint(void)
{
	EGUIControl::FMPaint();

	float spacing = decor->GetTextCtrlSpacing();
	const char *font_name =	decor->GetSmallFont();

	uiRect_s r  = GetFrameRect();
	
	uiRect_s tr = GetCtrlRect();

	float  float_val = prev_val;//(float)atof(value.c_str());
	float_val	=	Clamp<float>(float_val, min_val, max_val);

	IImage	*slider_scale	=	rs->RegisterImage(SLIDER_SCALE);
	IImage	*slider_bullet	=	rs->RegisterImage(SLIDER_BULLET);
	float scale_w = slider_scale->GetWidth();
	float scale_h = slider_scale->GetHeight();
	float bullet_w = slider_bullet->GetWidth();
	float bullet_h = slider_bullet->GetHeight();

	float percentage    = (float_val - min_val) / (max_val - min_val);
	float bullet_offset = percentage * scale_w;
	
	float start_pos	=	tr.x + (tr.w-scale_w)/2.0f;
	
	rs->DrawImage(ceil(start_pos),									 ceil(tr.y + (tr.h-scale_h)/2.0f),  SLIDER_SCALE);
	rs->DrawImage(ceil(start_pos),									 ceil(tr.y + (tr.h-scale_h)/2.0f),  SLIDER_GLOW, EVec4(1,1,1, percentage));
	rs->DrawImage(ceil(start_pos - 0.5*bullet_w + bullet_offset),    ceil(tr.y + (tr.h-bullet_h)/2.0f), SLIDER_BULLET);
	//decor->DrawBorderRect(tr.x, tr.y, tr.w, tr.h, "ui/border_outline_small.tga",	decor->GetControlColorHighlight());
	//decor->DrawBorderRect(tr.x+2, tr.y+2, 2*tr.h-8, tr.h-4, "ui/border_solid_small.tga",	decor->GetControlColorHighlight());
	
	/*if (int_val) {	
		decor->DrawTextInRect(tr.x, tr.y, tr.w, tr.h, font_name, "Yes", EVec4(1,1,1,1), GUI_ALIGN_LEFT);
	} else {
		decor->DrawTextInRect(tr.x, tr.y, tr.w, tr.h, font_name, "No", EVec4(1,1,1,1), GUI_ALIGN_LEFT);
	}  */
}


#endif












